import {
	STORE_TOKEN,
	STORE_HOMEMENU,
	LOGIN_IN,
	CHANGE_AREACODE
} from '../types.js';
import {
	login,
	register
} from '@/api/user.js';

const access_token =
	'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlcyI6IlVTRVIiLCJpc3MiOiJ0Y3poLWp3dC1jb21tb24iLCJleHAiOjE2MDk0MDY0NTcsImlhdCI6MTYwODgwMTY1NywidXNlckNvZGUiOiJNRU1CMjAyMDEyMjQwNDEwMDU4MzA4In0.97GtgG5_uX4k1hOKLvQvHr341PnfUoqU8yHPPqWjhD0';
const userCode = 'MEMB202012240410058308';
const expire_time = 1609406457807;

export const userActions = {
	// 密码 验证码登录
	login(context, payload) {
		return new Promise(async (resolve, reject) => {
			const result = await login(payload);
			if (result?.resultCode === '200') {
				const data = result?.data??{};
				context.commit(LOGIN_IN, data);
				resolve();
			} else {
				reject(result);
			}
		})
	},
	// 注册并登录
	register(context, payload) {
		return new Promise(async (resolve, reject) => {
			const result = await register(payload);
			if (result?.resultCode === '200') {
				const data = result?.data??{};
				// 保存登录信息
				context.commit(LOGIN_IN, data);
				resolve();
			} else {
				reject(result);
			}
		})
	},
	changeAreaCode(context, msg) {
	  if (context && msg && msg.cityName) {
		uni.$emit('update-city', msg.cityName);
		context.commit(CHANGE_AREACODE, msg);
	 }
	}
}
